package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entry.OrderForm;
import com.example.entry.Product;
import com.example.entry.ProductMessage;
import com.example.entry.User;
import com.example.service.IOrderFormService;
import com.example.service.IProductMessageService;
import com.example.service.IProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 * 商品前台控制器
 * */
@RequestMapping("/web/product")
@RestController
public class ProductWebController {

    @Resource
    private IProductService productService;

    @Resource
    private IProductMessageService productMessageService;

    @Resource
    private IOrderFormService orderFormService;

    /*
     * 根据商品id查询商品数据
     * */
    @GetMapping("/find-product/id={id}")
    public Result findProductById(@PathVariable Integer id){

        Product productById = productService.getById(id);
        return Result.success("success",productById);
    }

    /*
    * 充值金额
    * */
    @PostMapping("/recharge-amount")
    public Result rechargeAmount(@RequestBody User user){
        return Result.success();
    }

    /*
     * 根据商品id查询商品留言数据
     * */
    @GetMapping("/list-pro-msg/id={id}")
    public Result findProductMessageList(@PathVariable Integer id){
        QueryWrapper<ProductMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", id);
        List<ProductMessage> productMessageList = productMessageService.list(wrapper);
        return Result.success("success", productMessageList);
    }


    /*
    * 前台分页查询商品数据
    * */
    @GetMapping("/page")
    public Result getWebProductPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String name){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(!"".equals(name)){
            wrapper.like("name", name);
        }
        Page<Product> page = productService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success("请求成功", page);
    }

    @PostMapping("/product-message")
    public Result productMessage(@RequestBody ProductMessage productMessage){
        OrderForm orderForm = orderFormService.getById(productMessage.getOrderId());
        orderForm.setIsMsg(1);
        orderFormService.updateById(orderForm);
        productMessageService.save(productMessage);
        return Result.success("留言成功");
    }
}
